package com.nbsaas.boot.wms.rest.resource;

import com.nbsaas.boot.wms.api.apis.WarehouseCatalogApi;
import com.nbsaas.boot.wms.data.entity.WarehouseCatalog;
import com.nbsaas.boot.wms.api.domain.request.WarehouseCatalogRequest;
import com.nbsaas.boot.wms.api.domain.response.WarehouseCatalogResponse;
import com.nbsaas.boot.wms.api.domain.simple.WarehouseCatalogSimple;
import com.nbsaas.boot.wms.rest.convert.WarehouseCatalogSimpleConvert;
import com.nbsaas.boot.wms.rest.convert.WarehouseCatalogEntityConvert;
import com.nbsaas.boot.wms.rest.convert.WarehouseCatalogResponseConvert;
import com.nbsaas.boot.wms.data.repository.WarehouseCatalogRepository;

import java.io.Serializable;

import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;

import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.wms.api.domain.request.WarehouseCatalogSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;

import java.util.List;

/**
 * 仓库-业务接口实现
 */
@Transactional
@Service
public class WarehouseCatalogResource extends BaseResource<WarehouseCatalog, WarehouseCatalogResponse, WarehouseCatalogSimple, WarehouseCatalogRequest> implements WarehouseCatalogApi {

    @Resource
    private WarehouseCatalogRepository warehouseCatalogRepository;

    @Override
    public JpaRepositoryImplementation<WarehouseCatalog, Serializable> getJpaRepository() {
        return warehouseCatalogRepository;
    }

    @Override
    public Function<WarehouseCatalog, WarehouseCatalogSimple> getConvertSimple() {
        return new WarehouseCatalogSimpleConvert();
    }

    @Override
    public Function<WarehouseCatalogRequest, WarehouseCatalog> getConvertForm() {
        return new WarehouseCatalogEntityConvert();
    }

    @Override
    public Function<WarehouseCatalog, WarehouseCatalogResponse> getConvertResponse() {
        return new WarehouseCatalogResponseConvert();
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<WarehouseCatalogSimple> list(PageRequest request) {
        WarehouseCatalogSimpleConvert convert = new WarehouseCatalogSimpleConvert();
        if (request instanceof WarehouseCatalogSearch) {
            WarehouseCatalogSearch searchRequest = (WarehouseCatalogSearch) request;
            convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request, convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<WarehouseCatalogSimple> root(WarehouseCatalogSearch search) {
        ListResponse<WarehouseCatalogSimple> result = new ListResponse<>();
        List<WarehouseCatalogSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


